package com.huarui.quotation.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.ewaf.framework.service.BaseService;

import com.huarui.area.dao.IAreaDao;
import com.huarui.area.model.Area;
import com.huarui.company.dao.ICompanyDao;
import com.huarui.company.model.Company;
import com.huarui.company.model.CompanyMarket;
import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.service.DictionaryReadOnlyService.DictionaryResource;
import com.huarui.ip.dao.IIpAddressDao;
import com.huarui.ip.model.IpAddress;
import com.huarui.partner.service.PartnerService;
import com.huarui.quotation.dao.IQuotationDao;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;


public class QuotationSearchService extends BaseService{
	private Logger logger = Logger.getLogger(QuotationSearchService.class);
	
	private IAreaDao areaDao;
	
	private IIpAddressDao ipAddressDao;
	
	private ICompanyDao companyDao;
	
//	private PartnerService partnerService;
	
	public void setCompanyDao(ICompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	private IQuotationDao getDao() {
		return (IQuotationDao) super.dao;
	}
	
	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public void setIpAddressDao(IIpAddressDao ipAddressDao) {
		this.ipAddressDao = ipAddressDao;
	}


//	public void setPartnerService(PartnerService partnerService) {
//		this.partnerService = partnerService;
//	}

	/**
	 * 搜索零件报价信息(已经登录用户)
	 * @param paramsMap
	 * @return
	 */
	public Map<String, Object> searchQuotation(Map<String, Object> paramsMap) {
		
		SessionMessage se = UserHolder.getCurrentSessionMessage();
				
		Integer ownerId = se.getCompanyId();
		paramsMap.put("ownerId", ownerId);
		
//		Company comp = (Company) this.companyDao.getById(ownerId);
		
		List<Integer> marketIds = new ArrayList<Integer>();
		paramsMap.put("marketIds", marketIds);
		
		
//		paramsMap.put("paid", Company.PAID_YES);
		
		/*int realType = comp.getRealType();
		int paid = comp.getPaid();
		//认证并且已经付费
		if(Company.AUTHENTICATION_TYPE_COMPANY == realType && Company.PAID_YES == paid) {
			
			List<CompanyMarket> cms = this.companyDao.getCompanyMarketsByCompId(compId);
			
			Date currentTime = Calendar.getInstance().getTime();
			
			for(CompanyMarket cm : cms) {
				if(currentTime.after(cm.getGmtBegin()) && 
						currentTime.before(cm.getGmtEnd())) {
					
					Integer marketId = cm.getMarketId();
					marketIds.add(marketId);
				}
			}
			
		} 
		//没有认证 或者 认证了没有付费
		else {
			
			Long ip = (Long) paramsMap.get("ip");
			IpAddress ipAddress = this.ipAddressDao.getIpAddressByIp(ip);
			
			if(ipAddress == null) {
				logger.error("ip地址不正确，未找到相关零件报价信息");
				paramsMap.clear();
				return paramsMap;
			}
			
			Area area = (Area)this.areaDao.getById(ipAddress.getAreaId());
			if(area == null) {
				logger.error("地区信息不正确，未找到相关零件报价信息");
				paramsMap.clear();
				return paramsMap;
			}
			
			marketIds.add(area.getMarketId());
			
		}*/
		
		Integer totalProperty = this.getDao().searchQuotationCountByComplex(paramsMap);
		List<Map<String, Object>> list = this.getDao().searchQuotationByComplex(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("totalProperty", totalProperty);
		paramsMap.put("root", list);
		
		return paramsMap;
	}
	
	/**
	 * 首页搜索(根据用户ip，找到省份，只搜索这个省份的报价)
	 * @param paramsMap
	 * @return
	 */
	public Map<String, Object> searchQuotationOnFirstPage(Map<String, Object> paramsMap) {
		
//		Long ip = (Long) paramsMap.get("ip");
//		IpAddress ipAddress = this.ipAddressDao.getIpAddressByIp(ip);
//		
//		if(ipAddress == null) {
//			logger.error("ip地址不正确，未找到相关零件报价信息");
//			paramsMap.clear();
//			return paramsMap;
//		}
//		
//		Area area = (Area)this.areaDao.getById(ipAddress.getAreaId());
//		if(area == null) {
//			logger.error("地区信息不正确，未找到相关零件报价信息");
//			paramsMap.clear();
//			return paramsMap;
//		}
//		
//		Integer provinceId = area.getProvinceId();
//		paramsMap.put("provinceId", provinceId);
//		
//		SessionMessage se = UserHolder.getCurrentSessionMessage();
//		Integer compId = se.getCompanyId();
//		paramsMap.put("companyId", compId);
		
		Integer totalProperty = this.getDao().searchQuotationCountByComplex(paramsMap);
		List<Map<String, Object>> list = this.getDao().searchFirstPageQuotationByComplex(paramsMap);
		
		for(Map<String, Object> map : list) {
			String sourceName = DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_SOURCE, map.get("source").toString());
			map.put("source", sourceName);
			
			String retailPrice = String.valueOf(map.get("retailPrice"));
			if(StringUtils.isNotBlank(retailPrice)) {
				int index = retailPrice.indexOf(".");
				if(retailPrice.length() > index+2) {
					retailPrice = retailPrice.substring(0, index+3);
					map.put("retailPrice", retailPrice);
				}
			}
			
		}
		
		paramsMap.clear();
		paramsMap.put("totalProperty", totalProperty);
		paramsMap.put("root", list);
		
		return paramsMap;
	}
	
	/**
	 * 查看某公司所有报价
	 * @param paramsMap
	 * @return
	 */
	public Map<String, Object> queryCompanyQuotation(Map<String, Object> paramsMap) {
		
		SessionMessage se = UserHolder.getCurrentSessionMessage();
		Integer ownerId = se.getCompanyId();
		paramsMap.put("ownerId", ownerId);
		
		Integer totalProperty = this.getDao().searchQuotationCountByComplex(paramsMap);
		List<Map<String, Object>> list = this.getDao().searchQuotationByComplex(paramsMap);
		
		paramsMap.clear();
		paramsMap.put("totalProperty", totalProperty);
		paramsMap.put("root", list);
		
		return paramsMap;
	}
	
}
